package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesSaleOrder;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesSaleOrderDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:35:20
 */
public class MesSaleOrderConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesSaleOrderConverts.class);

    public static List<MesSaleOrderDTO> convert(List<MesSaleOrder> mesSaleOrderList) {
        if (CollectionUtils.isEmpty(mesSaleOrderList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesSaleOrderDTO> mesSaleOrderListResult = Lists.newArrayList();
        for (MesSaleOrder mesSaleOrder : mesSaleOrderList) {
            mesSaleOrderListResult.add(convert(mesSaleOrder));
        }
        return mesSaleOrderListResult;
    }

    public static MesSaleOrderDTO convert(MesSaleOrder mesSaleOrder) {
        try {
            return BeanUtils.copy(mesSaleOrder, MesSaleOrderDTO.class);
        } catch (Exception e) {
            LOG.error("MesSaleOrder到MesSaleOrderDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesSaleOrder到MesSaleOrderDTO转换失败", e);
        }
    }

    public static MesSaleOrder convert(MesSaleOrderDTO mesSaleOrderDTO) {
        try {
            return BeanUtils.copy(mesSaleOrderDTO, MesSaleOrder.class);
        } catch (Exception e) {
            LOG.error("MesSaleOrderDTO到MesSaleOrder转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesSaleOrderDTO到MesSaleOrder转换失败", e);
        }
    }
}